package com.code.demo.ui;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;

import com.code.demo.R;
import com.code.demo.databinding.ActivityMainBinding;
import com.code.demo.eventbus.EventBase;
import com.code.demo.ui.common.ToastFactory;
import com.code.demo.ui.gallery.GalleryActivity;
import com.code.demo.ui.guide.GuideActivity;
import com.code.demo.ui.test.ConstraintLayoutActivity;
import com.code.demo.ui.test.CoordinatorLayoutActivity;
import com.code.demo.ui.test.TestActivity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

import me.leolin.shortcutbadger.ShortcutBadger;

/**
 * Created by android on 2018/11/30.
 */

public class MainActivity extends AppCompatActivity {

    private ActivityMainBinding binding;
    private long exitTime = 0;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        binding.setPresenter(new Presenter());

        ShortcutBadger.applyCount(this, 1);

        EventBus.getDefault().register(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(EventBase event) {
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exit();
            return false;
        }

        return super.onKeyUp(keyCode, event);
    }

    public void exit() {

        if ((System.currentTimeMillis() - exitTime) > 2000) {
            ToastFactory.showToast(MainActivity.this, "再按一次退出程序");

            exitTime = System.currentTimeMillis();
        } else {
            finish();
        }
    }

    public class Presenter {
        public void onDemo() {
            startActivity(new Intent(MainActivity.this, TestActivity.class));
        }
        public void onGuide() {
            startActivity(new Intent(MainActivity.this, GuideActivity.class));
        }
        public void onGallery() {
            Intent intent = new Intent(MainActivity.this, GalleryActivity.class);
            ArrayList<String> list = new ArrayList<>();
            list.add("https://b-ssl.duitang.com/uploads/item/201510/07/20151007144332_T2ZEh.thumb.700_0.jpeg");
            list.add("https://img.zcool.cn/community/01a48856b5d21d32f875520fcfde2f.jpg@1280w_1l_2o_100sh.jpg");
            intent.putStringArrayListExtra("data", list);
            intent.putExtra("select", 0);
            intent.putExtra("showNumber", true);
            startActivity(intent);
        }

        public void onWeb() {
            startActivity(new Intent(MainActivity.this, WebActivity.class));
        }

        public void onService() {
            startActivity(new Intent(MainActivity.this, ServiceActivity.class));
        }

        public void onIntentService() {
            startActivity(new Intent(MainActivity.this, IntentServiceActivity.class));
        }

        public void onConstraintLayout() {
            startActivity(new Intent(MainActivity.this, ConstraintLayoutActivity.class));
        }

        public void onCorrdinatorLayout() {
            startActivity(new Intent(MainActivity.this, CoordinatorLayoutActivity.class));
        }
    }
}
